package com.common.utils;

import java.io.Serializable;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

public class CommonUtils implements Serializable {

    public static Map list2PageMap(List list) {
        Map result = new HashMap();
        result.put("total", Integer.valueOf(list.size()));
        result.put("rows", list);
        return result;
    }

    public static String getCurrentTime() {
        String returnStr = null;
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        java.util.Date date = new java.util.Date();
        returnStr = f.format(date);
        return returnStr;
    }

    public static String getCurrentTime(String format) {
        String returnStr = null;
        SimpleDateFormat f = new SimpleDateFormat(format);
        java.util.Date date = new java.util.Date();
        returnStr = f.format(date);
        return returnStr;
    }

    public static String getCurDate() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat simpledateformat = new SimpleDateFormat("yyyy-MM-dd");
        String strDate = simpledateformat.format(calendar.getTime());
        return strDate;
    }

    public static String formatDate(java.util.Date date, String format) {
        SimpleDateFormat f = new SimpleDateFormat(format);
        return f.format(date);
    }

    public static java.util.Date parseDate(String datestr, String format) {
        try {
            SimpleDateFormat f = new SimpleDateFormat(format);
            return f.parse(datestr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static java.util.Date getChangeDate(java.util.Date date, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(5, amount);

        return calendar.getTime();
    }

    public static boolean isEmpty(String string) {
        return StringUtils.isEmpty(string);
    }

    public static boolean isNotEmpty(String string) {
        return StringUtils.isNotEmpty(string);
    }

    @SuppressWarnings("unchecked")
    public static <T> T convertValue(Object value, Class<T> clazz) {
        if (value == null)
            return null;
        String param = value.toString();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            if (param.trim().equals("")) {
                return null;
            }

            if (clazz == Integer.class) {
                return (T) Integer.valueOf(param);
            }
            if (clazz == Double.class) {
                return (T) Double.valueOf(param);
            }
            if (clazz == Float.class) {
                return (T) Float.valueOf(param);
            }
            if (clazz == Long.class) {
                return (T) Long.valueOf(param);
            }
            if (clazz == String.class) {
                return (T) param;
            }
            if (clazz == java.util.Date.class) {
                return (T) dateFormat.parse(param);
            }
            if (clazz == Timestamp.class) {
                return (T) new Timestamp(dateFormat.parse(param).getTime());
            }
            if (clazz == java.sql.Date.class) {
                return (T) new java.sql.Date(dateFormat.parse(param).getTime());
            }
            if (clazz == Boolean.class) {
                return (T) Boolean.valueOf(value.toString());
            }

            return clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
            return null;
        } catch (IllegalAccessException e) {
            return null;
        } catch (ParseException e) {
        }
        return null;
    }

    public static boolean isIE(HttpServletRequest request) {
        String userAgent = request.getHeader("USER-AGENT").toLowerCase();
        boolean isIe = true;
        int index = userAgent.indexOf("msie");
        if (index == -1) {
            isIe = false;
        }
        return isIe;
    }

    public static boolean isChrome(HttpServletRequest request) {
        String userAgent = request.getHeader("USER-AGENT").toLowerCase();
        boolean isChrome = true;
        int index = userAgent.indexOf("chrome");
        if (index == -1) {
            isChrome = false;
        }
        return isChrome;
    }

    public static boolean isFirefox(HttpServletRequest request) {
        String userAgent = request.getHeader("USER-AGENT").toLowerCase();
        boolean isFirefox = true;
        int index = userAgent.indexOf("firefox");
        if (index == -1) {
            isFirefox = false;
        }
        return isFirefox;
    }

    public static String getClientExplorerType(HttpServletRequest request) {
        String userAgent = request.getHeader("USER-AGENT").toLowerCase();
        String explorer = "非主流浏览器";
        if (isIE(request)) {
            int index = userAgent.indexOf("msie");
            explorer = userAgent.substring(index, index + 8);
        } else if (isChrome(request)) {
            int index = userAgent.indexOf("chrome");
            explorer = userAgent.substring(index, index + 12);
        } else if (isFirefox(request)){
            int index = userAgent.indexOf("chrome");
            explorer = userAgent.substring(index, index + 8);
            }
        return explorer.toUpperCase();
    }
}